<template>
  <view class="container">
    <uv-text text="用户注册" size="3rem" color="#fff" align="center" class="title" />
    <view class="container-form">
      <uv-form labelPosition="left" ref="registerFormRef" :model="registerForm" :rules="rules" labelWidth="auto">
        <uv-form-item prop="phone" borderBottom>
          <uv-input prefixIcon="account-fill" placeholderStyle="color: #fff;margin-left:2rem"
            v-model="registerForm.phone" placeholder="请输入手机号码" border="none" />
        </uv-form-item>
        <uv-form-item prop="password" borderBottom>
          <uv-input prefixIcon="lock-fill" placeholderStyle="color: #fff;margin-left:2rem"
            v-model="registerForm.password" type="password" placeholder="请输入密码" border="none" />
        </uv-form-item>
        <uv-form-item prop="confirmPassword" borderBottom>
          <uv-input prefixIcon="lock-fill" placeholderStyle="color: #fff;margin-left:2rem"
            v-model="registerForm.confirmPassword" type="password" placeholder="请确认密码" border="none" />
        </uv-form-item>
        <uv-button type="info" text="注册" :loading="loading" loadingText="注册中" @click="handleRegister" class="button" />
      </uv-form>
    </view>
    <view class="login-link">
      <uv-text style="justify-content: center;text-decoration: underline;" color="blue" text="已有账号？返回登录"
        @click="jumpToLogin" />
    </view>
  </view>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { register } from '@/api/login'
import { toast } from '@/utils/common'

const registerForm = reactive({
  phone: '',
  password: '',
  confirmPassword: ''
})

const rules = ref({
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度6-20位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule: any, value: string, callback: Function) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
})

const loading = ref<boolean>(false)
const registerFormRef = ref<any>()

const handleRegister = async () => {
  const valid = await registerFormRef.value.validate()
  if (!valid) return

  loading.value = true
  try {
    await register({
      phone: registerForm.phone,
      password: registerForm.password
    })
    toast('注册成功')
    uni.navigateBack()
  } catch (error) {
    toast(error.tips || '注册失败')
  } finally {
    loading.value = false
  }
}
const jumpToLogin = () => {
  uni.navigateTo({ url: '/pages/login/index' })
}
</script>

<style lang="scss" scoped>
.container {
  height: 100vh;
  width: 100%;
  background-image: url('/static/bg.jpg');
  background-size: cover;
  overflow: hidden;

  .title {
    margin-top: 20% !important;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5)
  }

  .container-form {
    width: 80%;
    max-width: 400px;
    margin: 0 auto;
    margin-top: 40%;
    background-color: rgb(227 222 222 / 50%);
    border-radius: 1rem;
    padding: 1rem;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);

    ::v-deep .uni-input-input {
      margin-left: 2rem;
    }

    .button {
      margin-top: 10rpx;
    }
  }

  .login-link {
    margin-top: 20rpx;
    text-align: center;
    color: #fff;
  }
}
</style>